package com.example.start

import android.media.MediaPlayer
import android.os.Bundle
import android.view.MenuItem
import android.widget.Button
import android.widget.MediaController
import android.widget.SeekBar
import android.widget.TextView
import android.widget.VideoView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import java.io.IOException

class NationalInformationActivity : AppCompatActivity() {
    private lateinit var mediaPlayer: MediaPlayer
    private lateinit var seekBar: SeekBar
    private lateinit var textView : TextView
    private var isSeekBarChanging = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_national_information)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        var actionBar = supportActionBar
        actionBar?.setHomeButtonEnabled(true)
        actionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setTitle("国旗与国歌")
        var mVideo = findViewById<VideoView>(R.id.nationAnthemVideoView)
        val mediaController = MediaController(this@NationalInformationActivity)
        //VideoView绑定控制器
        mVideo.setMediaController(mediaController);
        //设置视频地址
        mVideo.setVideoPath("https://www.gov.cn/guoqing/guoge/guoge.mp4")
        //获取焦点
        mVideo.requestFocus();
        //播放视频
        mVideo.start();

        textView = findViewById(R.id.anthemProgressTextView)
        seekBar = findViewById(R.id.anthemSeekBar)
        mediaPlayer = MediaPlayer.create(this, R.raw.national_anthem)
        seekBar.max = mediaPlayer.duration
        val playButton = findViewById<Button>(R.id.playAnthemButton)
        playButton.setOnClickListener {
            if (!mediaPlayer.isPlaying) {
                mediaPlayer.start()
            }
        }
        val pauseButton = findViewById<Button>(R.id.pauseAnthemButton)
        pauseButton.setOnClickListener {
            if (mediaPlayer.isPlaying) {
                mediaPlayer.pause()
            }
        }
        Thread {
            while (mediaPlayer != null) {
                if (mediaPlayer.isPlaying) {
                    try {
                        // 更新SeekBar进度
                        val currentPosition = mediaPlayer.currentPosition
                        if (!isSeekBarChanging) {
                            runOnUiThread { seekBar.progress = currentPosition
                                textView.text = millisecondsToTime(currentPosition)}
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                try {
                    Thread.sleep(1000)
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                }
            }
        }.start()

        seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {}

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                isSeekBarChanging = true
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                mediaPlayer.seekTo(seekBar.progress)
                isSeekBarChanging = false
            }
        })

    }

    override fun onDestroy() {
        super.onDestroy()
        mediaPlayer.stop()
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId)
        {
            android.R.id.home->
            {
                this.finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
    // 将毫秒转换为时间格式字符串
    private fun millisecondsToTime(milliseconds: Int): String {
        val seconds = milliseconds / 1000
        val minutes = seconds / 60
        val remainingSeconds = seconds % 60
        return String.format("%02d:%02d", minutes, remainingSeconds)
    }
}